package com.lbx.config;

import com.lbx.entity.SysUser;
import com.lbx.mapper.SysUserMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author lbx create by 2021/6/8 11:11
 * @apiNote SecurityConfig
 */
@Configuration
@EnableGlobalMethodSecurity(securedEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Resource
    private MyUserDetailsService myUserDetailsService;
    @Resource
    private BCryptPasswordEncoder bCryptPasswordEncoder;
    @Resource
    private SysUserMapper sysUserMapper;
    @Resource
    private FailureHandler failureHandler;
    @Resource
    private SuccessHandler successHandler;
    
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().antMatchers("/**").permitAll().and().httpBasic().disable();
        http.formLogin()
                .loginPage("/loginPage")
                .loginProcessingUrl("/login")
                .successHandler(successHandler)
                .usernameParameter("username")
                .passwordParameter("password")
                .failureHandler(failureHandler)
                .permitAll();
        http.cors().disable().csrf().disable();
        http.headers().frameOptions().disable();
    }
    
    /*
     * The 'Access-Control-Allow-Origin' header has a value 'http://lbx01.com' that is not equal to the supplied origin.
     * */
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(myUserDetailsService).passwordEncoder(bCryptPasswordEncoder);
    }
    
    @Bean
    public BCryptPasswordEncoder bCryptPasswordEncoder() {
        return new BCryptPasswordEncoder();
    }
    
    @Service
    private class MyUserDetailsService implements UserDetailsService {
        @Override
        public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
            SysUser sysUser = sysUserMapper.getOneByUsername(username);
            if (sysUser == null) {
                throw new UsernameNotFoundException("this username not found!");
            }
            return sysUser;
        }
    }
}
